<template>
    <div class="front bj">
        <el-scrollbar style="height: 100%;">
            <el-form label-width="100px" label-position="left" :model="formData" ref="formRef" :rules="formRules" style="overflow: hidden;margin-top: 12px !important;">
                <el-form-item label="名　　称：" prop="name" style="margin-top: 10px;">
                    <el-input v-model.trim="formData.name" @keyup.enter.native="enterKey($event, 'DPT1')"></el-input>
                </el-form-item>
                <el-form-item label="助 记 码：" prop="mnemonicCode">
                    <el-input v-model.trim="formData.mnemonicCode" ref="DPT1" @keyup.enter.native="enterKey($event, 'DPT2')"></el-input>
                </el-form-item>
                <el-form-item label="上级部门：" class="readonly" prop="parentId">
                    <el-input v-model="parentName[0]" readonly @click.native="selectDepartment"></el-input>
                    <el-input v-model="formData.parentId" readonly style="display: none;"></el-input>
                </el-form-item>
                <el-form-item label="部门类型：" prop="category">
                    <el-select v-model="formData.category" filterable placeholder="选择部门类型" ref="DPT2" @keyup.enter.native="enterKey($event, 'DPT3')" style="width: 100%;">
                        <el-option v-for="item in departmentType" :key="item['id']" :label="item['detailedName']" :value="String(item['id'])"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="部门主管：" prop="supervisor">
                    <el-select v-model="formData.supervisor" clearable filterable placeholder="请选择部门主管" ref="DPT3" @keyup.enter.native="enterKey($event, 'DPT4')" style="width: 100%;">
                        <el-option v-for="item in supervisor" :key="item['id']" :label="item['name']" :value="item['id']"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="备注：" prop="remark">
                    <el-input type="textarea" ref="DPT4" v-model.trim="formData.remark" placeholder="输入备注" @keyup.enter.native="save('formRef')"></el-input>
                </el-form-item>
                <el-form-item label="连续新增：" style="margin-bottom: 10px;" v-if="!this.rowId">
                    <el-checkbox v-model="continueAdd"></el-checkbox>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" size="small" @click="save('formRef')">保存</el-button>
                    <el-button type="danger" size="small" @click="close">关闭</el-button>
                </el-form-item>
            </el-form>
        </el-scrollbar>
    </div>
</template>
<script>
export default {
	data() {
		return {
			continueAdd: false,
			formData: {
                parentId: ''
            },
			formRules: {
                name: [{required: true, message: '名称不能为空', triggle: 'blur'}],
                parentId: [{required: true, message: '上级部门不能为空', triggle: 'blur'}],
                category: [{required: true, message: '部门类型不能为空', triggle: 'change'}]
            },
			departmentType: this.$store.state.departmentStore.departmentType,			//部门类型
			supervisor: this.$store.state.departmentStore.supervisor,			//部门主管
			parentName: this.$store.state.departmentStore.parentName,		//上级部门名称
            parentId: this.$store.state.departmentStore.parentId,           //上级部门id
		}
	},
    computed: {
        systemId() {
            return this.$store.state.systemId;
        },
        rowId() {
            return this.$store.state.departmentStore.rowId;
        }
    },
    watch: {
        parentId(newVal) {
            this.formData['parentId'] = newVal[0].toString();
        }
    },
	methods: {
        getUpdateData() {
            this.$http.get('department/selectObjectById?id=' + this.rowId).then(res => {
                if(res.data.code == 20001) {
                    if(res.data.data) {
                        res.data.data['category'] = res.data.data['category'] && res.data.data['category'].toString();
                        this.formData = res.data.data;
                        this.formData['parentId'] = String(this.formData['parentId']);
                        if(this.supervisor.length <= 0) this.formData['supervisor'] = '';           //如果部门主管数据为空，清空部门主管id,防止找不到数据而显示数字
                    }
                }
            });
        },
		save(formName) {      //保存
			this.$refs[formName].validate((valid) => {
                if (valid) {
                    let data = {
                        name: this.formData['name'],
                        mnemonicCode: this.formData['mnemonicCode'],
                        parentId: this.formData['parentId'] == this.systemId ? 0 : this.formData['parentId'],
                        category: this.formData['category'],
                        supervisor: this.formData['supervisor'],
                        remark: this.formData['remark'],
                        systemId: this.systemId
                    }
                    if(this.formData['id']) data['id'] = this.formData['id'];       //如果是修改，加上修改id
                    this.$http({
                        url: 'department/insertOrUpdateDepartment',
                        method: 'post',
                        params: data
                    }).then(res => {
                        if(res.data.code == 20001) {
                            this.$message({message: res.data.data || (this.rowId ? '新增成功' : '修改成功'), type: 'success', duration: 1500});
                            this.$store.commit('departmentStore/updateExpandNodeId', parseInt(data['parentId']) == 0 ? this.systemId : parseInt(data['parentId']));
                            setTimeout(() => {
                                if(this.rowId) {
                                    this.close();
                                } else {
                                    //修改
                                    this.continueAdd ? this.$refs[formName].resetFields() : this.close();
                                }
                                this.$store.dispatch('departmentStore/getTreeData');        //刷新树形数据
                            }, 1000);
                        }
                    });
                }
            });
		},
        selectDepartment() {
            this.$util.openLayerIframe({title: '所有部门', zIndex: 13, width: '450px', height: '360px', url: this.$domain + '/#/basicData/department/selectDepartment'});
        },
		close() {           //关闭新增弹窗
            let index = parent.layer.getFrameIndex(window.name);    //先得到当前iframe层的索引
            parent.layer.close(index);
        },
        enterKey(lastRef, nextRef) {		//回车键换行
            if(lastRef.target.value) {
                this.$refs[nextRef].focus();
            }
        }
	},
    mounted() {
        if(this.rowId) this.getUpdateData();
        let parentId = this.$store.state.departmentStore.parentId;
        this.formData['parentId'] = parentId.toString();
    }
}
</script>